﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.Configuration;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;



namespace DotNetNuke.Tests.UI.WatiN.AdministratorTests
{
    [TestFixture]


    public class AdminLocalizationTests : ATBase
    {

        #region Test Setup and TearDown

        public bool LanguagePackInstalled = false;

        [SetUp]
        public void SetUp()
        {

            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            if (LanguagePackInstalled)
            {
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                LoginPage loginPage = new LoginPage(homePage);
                if (!homePage.RegisterLink.Text.Contains(TestUsers.Host.DisplayName))
                {
                    if (!homePage.RegisterLink.Text.Equals("Register"))
                    {
                        loginPage.LogoffUser();
                    }
                    loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                }
                //Uninstall the language pack and delete the files
                ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
                ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
                extensionsPage.ExtensionPackageSelectList.Select("Core Language Pack");
                System.Threading.Thread.Sleep(2000);
                extensionsPage.GetExtensionUninstallButton(LocaleName).Click();
                System.Threading.Thread.Sleep(2000);
                extensionsPage.DeleteExtensionFilesCheckbox.Checked = true;
                ConfirmDialogHandler dialog = new ConfirmDialogHandler();
                using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
                {
                    extensionsPage.UninstallPackageLink.ClickNoWait();
                    dialog.WaitUntilExists();
                    dialog.OKButton.Click();
                }
                ieInstance.WaitForComplete();
                LanguagePackInstalled = false;
            }

            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        public void Change_Portal_Language_for_Annonymous_User_in_URL()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            ieInstance.GoTo(SiteURL + "/Host/Host%20Settings.aspx");
            HostSettingsPage hostSettingsPage = new HostSettingsPage(homePage);
            hostSettingsPage.HostEmailTextField.Value = TestUsers.Host.Email;
            hostSettingsPage.OtherSettingsExpandButton.Click();
            hostSettingsPage.EnableContentLocalizationCheckbox.Checked = true;
            hostSettingsPage.UpdateLink.Click();
            System.Threading.Thread.Sleep(1500);

            ieInstance.GoTo(SiteURL + "/Admin/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);

            //Install a language pack
            extensionsPage.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;

            //Enable Content Localization
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            ieInstance.WaitForComplete();
            languagePage.EnableLocalizedContentLink.Click();
            System.Threading.Thread.Sleep(500);
            languagePage.EnableLocalizedContentConfirmation.Click();
            Gallio.Framework.TestLog.WriteLine(languagePage.LanguageTable.CheckBoxes.Count + " Checkboxes in the table");
            Table languageTable = languagePage.LanguageTable;
            System.Threading.Thread.Sleep(2000);
            languageTable.CheckBoxes.Filter(Find.ById(s => s.EndsWith("publishedCheckbox")))[1].Checked = true;
            //languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Published").Checked = true;

            //Act 
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            Span flagSpan = languagePage.GetLanguageFlagSpanByLocaleName(LocaleName);
            flagSpan.Image(Find.ByAlt(LanguageCode)).Click();
            flagSpan = languagePage.GetLanguageFlagSpanByLocaleName(LocaleName);

            //Assert
            Assert.IsFalse(flagSpan.ClassName.Equals("Language Selected"));
            Assert.IsTrue(loginPage.LoginLink.Text.Contains("Connexion"));

        }


        #endregion
    }
}
